Imports System.Data.SqlClient
Imports NENAData.NENAMember
Imports NENAData.LINQ
Imports NENAData.NENAProvinces
Imports NENAData.Transactions

Public Class ApplicationFormCompleted
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblAffililateName As System.Web.UI.WebControls.Label
    Protected WithEvents lblAffiliateName As System.Web.UI.WebControls.Label
    Protected WithEvents lblAffiliateCheckPayable As System.Web.UI.WebControls.Label
    Protected WithEvents lblAffiliateFee As System.Web.UI.WebControls.Label
    Protected WithEvents lblAddress1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblCity As System.Web.UI.WebControls.Label
    Protected WithEvents lblProvince As System.Web.UI.WebControls.Label
    Protected WithEvents lblCountry As System.Web.UI.WebControls.Label
    Protected WithEvents lblPostalCode As System.Web.UI.WebControls.Label
    Protected WithEvents lblAddress2 As System.Web.UI.WebControls.Label
    Protected WithEvents lblContactName As System.Web.UI.WebControls.Label
    Protected WithEvents lblContactEmail As System.Web.UI.WebControls.Label
    Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
    Protected WithEvents lblApplicantName As System.Web.UI.WebControls.Label
    Protected WithEvents lblApplicantEmail As System.Web.UI.WebControls.Label
    Protected WithEvents lblRenewalMessage As System.Web.UI.WebControls.Label
    Protected WithEvents lbl_Error As System.Web.UI.WebControls.Label
    Protected WithEvents panelPayPal As System.Web.UI.WebControls.Panel
    Protected WithEvents imgPayPal As System.Web.UI.WebControls.Image

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SetPayPal()

        If Session("Member_ID") Is Nothing Then
            Session("Member_ID") = Request.QueryString("Member_ID")
        End If
        If Session("Member_ID") Is Nothing Then
            Response.Redirect("/", False)
        End If

        If Session("Affiliate_ID") Is Nothing Then
            Session("Affiliate_ID") = Request.QueryString("Affiliate_ID")
        End If

        If Session("Account_Status") Is Nothing Then
            If Request.QueryString("Account_Status") Is Nothing Then
                Session("Account_Status") = 1
            Else
                Session("Account_Status") = Request.QueryString("Account_Status")
            End If
        End If

        lblRenewalMessage.Text = "Once your payment has been recieved, you will be issued a reciept via email.<br/>" & _
            "If you pay by PayPal, you will be issued an immediate reciept.<br /> " & _
            "If you have already paid your fees for the current year, your account will be activated shortly - no further payment or action is necessary."

        Dim oMember As New NENAMember
        oMember.UpdateMemberApplication(Session("Member_ID"), Session("Account_Status"))

        'get applicant information
        GetApplicantInfo(Session("Member_ID"))
        'load the affiliate details
        LoadAffiliateDescription()
        'find the contact member id
        'load the affiliate contact info
        LoadAffiliateContact((GetContactMember(Session("Affiliate_ID"))))

        Try
            'write the email to the screen
            If Not Page.IsPostBack Then
                NENAData.Email.Message.ApplicationCompleted(Session("Member_ID"))
            End If
        Catch ex As Exception
            lbl_Error.Text = "Warning: We could not verify your email - please verify it is correct." & _
                "<br/>Please email the membership contact below if it is incorrect."
        End Try
    End Sub
    Private Sub LoadAffiliateDescription()
        Dim oNENAAffiliate As New NENAAffiliate
        Dim oAffiliate As Affiliate

        Dim intAffiliateID As Integer = CType(Session("Affiliate_ID"), Integer)
        oAffiliate = oNENAAffiliate.GetAffiliate_byAffiliateID(intAffiliateID)

        lblAffiliateName.Text = oAffiliate.Affiliate_Description
        lblAffiliateCheckPayable.Text = oAffiliate.Affiliate_Description
        lblAffiliateFee.Text = String.Format("{0,-10:C}", oAffiliate.Membership_Fee)
        Session("payment_amt") = String.Format("{0:00.00}", oAffiliate.Membership_Fee)
    End Sub
    Private Function GetContactMember(ByVal strAffiliateID As String) As String
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String
        Dim strMemberContactId As String = ""

        strSql = "select * from Directors_Provincal where " & _
       "affiliate_id = '" & strAffiliateID & "';"

        conPubs = Connections.GetSQLConnection()
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        While dtrTitles.Read
            strMemberContactId = dtrTitles("members_contact_id")
        End While

        dtrTitles.Close()
        conPubs.Close()
        Return strMemberContactId
    End Function
    Private Sub LoadAffiliateContact(ByVal strContactID As String)
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select * from members_committee Tmc, Directors_Provincal Tdp " & _
        "where Tdp.Members_Contact_ID = Tmc.ID And Tmc.ID = '" & strContactID & "';"

        'conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        conPubs = Connections.GetSQLConnection()
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        While dtrTitles.Read
            lblContactName.Text = dtrTitles("FirstName") & " " & dtrTitles("LastName")
            lblContactEmail.Text = dtrTitles("ContactEmail")
            'HomePhone.Text = dtrTitles("homephone")
            lblAddress1.Text = dtrTitles("address_1")
            lblAddress2.Text = dtrTitles("address_2")
            lblCity.Text = dtrTitles("city")
            lblPostalCode.Text = dtrTitles("postal_code")
            lblProvince.Text = dtrTitles("province")
            'lblCountry.Text = dtrTitles("country")
        End While
        dtrTitles.Close()
        conPubs.Close()
    End Sub
    Private Sub GetApplicantInfo(ByVal strMemberID As String)
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select * from members where " & _
       "member_id = '" & strMemberID & "';"

        conPubs = Connections.GetSQLConnection()
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        While dtrTitles.Read
            lblApplicantName.Text = dtrTitles("FirstName") & " " & dtrTitles("LastName")
            lblApplicantEmail.Text = dtrTitles("EmailAddress")
        End While
        dtrTitles.Close()
        conPubs.Close()
    End Sub
    Public Sub SetPayPal()
        'Response.Write("Member_ID = " + Session("Member_ID") + "<br />")
        'Response.Write("Affiliate_ID = " + Session("Affiliate_ID") + "<br />")
        'Response.Write("payment_amt = " + Session("payment_amt") + "<br />")

        Dim x As New NENAAffiliate
        Dim oAffiliate As Affiliate
        oAffiliate = x.GetAffiliate_byAffiliateID(CType(Session("Affiliate_ID"), Integer))

        If oAffiliate.PayPalEnabled Then
            panelPayPal.Visible = True
        Else
            panelPayPal.Visible = False
        End If

    End Sub
    Protected Sub imgPayPal_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        'Dim strRedirect As String = "http://localhost:49245/expresscheckout.aspx"
        Dim strRedirect As String = "paypal/expresscheckout.aspx"

        'todo: remove hardcoding transaction
        strRedirect &= "?Member_ID=" + Session("Member_ID").ToString
        strRedirect &= "&Affiliate_ID=" + Session("Affiliate_ID").ToString
        strRedirect &= "&payment_amt=" + Session("payment_amt").ToString
        strRedirect &= "&TransactionID_NENA=" + Convert.ToInt32(TransactionList.Transactions.Membership2012).ToString

        Response.Redirect(strRedirect, False)
    End Sub
End Class
